package me.obstsalatt.guildera2.core;

import me.obstsalatt.guildera2.container.GPlayer;
import me.obstsalatt.guildera2.global.Main;
import me.obstsalatt.guildera2.utility.Util;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashSet;

/**
 * Created with IntelliJ IDEA.
 * User: Robin
 * Date: 08.02.14, 16:44
 * Project: GuildEra2
 * Package: me.obstsalatt.guildera2
 *
 */
public class Guild {

    private String name;
    private String defaultRank;
    private HashSet<GuildRank> ranks;
    private Main main;
    private ArrayList<GPlayer> member;


    public Guild(Main main, String name) {
        this.name = name;
        this.main = main;
        ranks = new HashSet<GuildRank>();
        defaultRank = "member";
        member = new ArrayList<GPlayer>();
        load();
    }


    // Used to save the content of this guild to local file
    public void save() {
        //saving ranks and defaultRank
        saveRanks();

        //save player association
        savePlayerAssigns();
    }
    private void savePlayerAssigns() {
        String base = "";
        boolean first = true;
        for(GPlayer gply : member) {
            if(first) {
                base = gply.getPlayer().getName();
            } else {
                base = base + "#" + gply.getPlayer().getName();
            }
        }
        Util.writeFile(getGuildFilePath() + "playerAssigns.dat", base);
    }
    private void saveRanks() {
        for(GuildRank gr : ranks) {
            Util.writeFile(getGuildFilePath() + "ranks" + File.pathSeparator + gr.getName() + ".dat", gr.toString());
        }
        Util.writeFile(getGuildFilePath() + "ranks" + File.pathSeparator + "defaultRank.dat", defaultRank);
    }

    // Used to load the content of this guild from local files
    public void load() {
        //load ranks
        loadRanks();

        //load player associations
        loadPlayerAssigns();
    }
    private void loadRanks() {
        File folder = new File(getGuildFilePath() + "ranks");
        String input = "";
        for(File f : folder.listFiles()) {
            if(f.isFile()) {
                try {
                    input = Util.readFile(f.getAbsolutePath());
                } catch (IOException e) {
                    e.printStackTrace();
                }
                String[] fragments = input.split("#");
                ranks.add(new GuildRank(fragments[0], GuildRights.fromValue(Integer.parseInt(fragments[1]))));
            }
        }
        try {
            defaultRank = Util.readFile(getGuildFilePath() + "ranks" + File.pathSeparator + "defaultRank" + ".dat");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    private void loadPlayerAssigns() {
        String base = "";
        try {
            base = Util.readFile(getGuildFilePath() + "playerAssigns.dat");
        } catch (IOException e) {
            e.printStackTrace();
        }
        String[] fragments = base.split("#");
        for(String fragment : fragments) {
            member.add(main.getPlayerManager().getPlayer(fragment));
        }
    }

    // Administrate members
    public void addPlayer(String name) {
        GPlayer ply = main.getPlayerManager().getPlayer(name);
        ply.setGuild(this);
        member.add(ply);
    }
    public void removePlayer(String name) {
        GPlayer ply = main.getPlayerManager().getPlayer(name);
        member.remove(ply);
        ply.removeGuild();
    }

    // For internal use only!
    private void addRank(String name, int[] values) {
        ranks.add(new GuildRank(name, values));
    }
    private void alterRank(String name, int[] values) {
        removeRank(name);
        addRank(name, values);
    }
    private void removeRank(String name) {
        ranks.remove(new GuildRank(name));
    }

    public GuildRank getRank(String name) {
        for(GuildRank gr : ranks) {
            if(gr.getName().equals(name)) {
                return gr;
            }
        }
        return null;
    }


    // Getter
    public String getName() {
        return name;
    }
    public String getDefaultRank() {
        return defaultRank;
    }
    public String getGuildFilePath() {
        return  (main.getDataFolder().getAbsolutePath() + File.pathSeparator + "guildera2"
                                                        + File.pathSeparator + "guilds"
                                                        + File.pathSeparator + name
                                                        + File.pathSeparator);
    }
}
